﻿Imports System.Data.OleDb
Public Class frmNhanVienTiepThi
#Region "Form Event"
    Private Sub frmNhanVienTiepThi_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            NhanVienTiepThi_Load()
            'Phân quyền Form
            Dim aPermission(4) As Boolean
            aPermission = PhanQuyenForm("MenuNhanVienTiepThi", frmMain.UserId)
            cmdAddNew.Enabled = aPermission(0)
            cmdEdit.Enabled = aPermission(1)
            cmdDelete.Enabled = aPermission(2)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
#End Region
#Region "Load Data"
    Public Sub NhanVienTiepThi_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsNhanVienTiepThi As New DataSet
            Dim dtNhanVienTiepThi As New DataTable
            Dim query As String = "Select NhanVienTiepThiId,MaNhanVienTiepThi,TenNhanVienTiepThi,GhiChu From NhanVienTiepThi Order By TenNhanVienTiepThi ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsNhanVienTiepThi)
            dbConn.Close()
            dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
            dtNhanVienTiepThi.Columns.Add("STT")
            For i As Integer = 0 To dtNhanVienTiepThi.Rows.Count - 1
                dtNhanVienTiepThi.Rows(i)("STT") = i + 1
            Next
            dgvNhanVienTiepThi.AutoGenerateColumns = False
            dgvNhanVienTiepThi.DataSource = dtNhanVienTiepThi
            'set focus to first row
            If dgvNhanVienTiepThi.Rows.Count - 1 > 0 Then
                dgvNhanVienTiepThi.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region



    
    
    
    
    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            frmThemNhanVienTiepThi.NhanVienTiepThiId = -1
            ShowModalForm(frmThemNhanVienTiepThi, False, False)
            frmThemNhanVienTiepThi.NhanVienTiepThiId = -1
        Catch ex As Exception
            ShowError("Có lỗi trong quá trình xử lý.Xin vui lòng thực hiện lại", MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            frmThemNhanVienTiepThi.NhanVienTiepThiId = Integer.Parse(dgvNhanVienTiepThi.Rows(dgvNhanVienTiepThi.CurrentCell.RowIndex).Cells("NhanVienTiepThiId").Value.ToString)
            ShowModalForm(frmThemNhanVienTiepThi, False, False)
            frmThemNhanVienTiepThi.NhanVienTiepThiId =-1
        Catch ex As Exception
            ShowError("Có lỗi trong quá trình xử lý.Xin vui lòng thực hiện lại", MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim NhanVienTiepThiId As Integer = Integer.Parse(dgvNhanVienTiepThi.Rows(dgvNhanVienTiepThi.CurrentCell.RowIndex).Cells("NhanVienTiepThiId").Value.ToString)
            Dim TenNhanVienTiepThi As String = dgvNhanVienTiepThi.Rows(dgvNhanVienTiepThi.CurrentCell.RowIndex).Cells("TenNhanVienTiepThi").Value
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa  : " & TenNhanVienTiepThi, _
                  "Thông báo", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Warning)
            If dlgRes = DialogResult.OK Then
                Dim dataAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                Dim query As String = "Delete NhanVienTiepThi Where NhanVienTiepThiId=" & NhanVienTiepThiId
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                NhanVienTiepThi_Load()
            End If
        Catch ex As Exception
            ShowError("Có lỗi trong quá trình xử lý.Xin vui lòng thực hiện lại")
        End Try
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsNhanVienTiepThi As New DataSet
            Dim dtNhanVienTiepThi As New DataTable
            Dim query As String = "Select NhanVienTiepThiId,MaNhanVienTiepThi,TenNhanVienTiepThi,GhiChu From NhanVienTiepThi Where TenNhanVienTiepThi Like N'%" & txtTextSearch.Text & "%' Order By TenNhanVienTiepThi ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsNhanVienTiepThi)
            dbConn.Close()
            dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
            dtNhanVienTiepThi.Columns.Add("STT")
            For i As Integer = 0 To dtNhanVienTiepThi.Rows.Count - 1
                dtNhanVienTiepThi.Rows(i)("STT") = i + 1
            Next
            dgvNhanVienTiepThi.AutoGenerateColumns = False
            dgvNhanVienTiepThi.DataSource = dtNhanVienTiepThi
            'set focus to first row
            If dgvNhanVienTiepThi.Rows.Count - 1 > 0 Then
                dgvNhanVienTiepThi.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class